﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using WebServerConnectorDll;

namespace FruitManagerApp
{
    public class PermissionList
    {
        private readonly CStudy[] STUDIES = new CStudy[]
        {
            new CStudy{ StudyUID = EStudyUID.Personnel, Title = "员工管理设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Device, Title = "设备管理设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Office, Title = "营业部管理设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.User, Title = "用户管理设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Domino, Title = "商品管理设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Order, Title = "订单管理设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Message, Title = "留言管理设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Word, Title = "文章管理设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Image, Title = "相册管理设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
        };

        private readonly CSeries[] SERIESES = new CSeries[]
        {
            new CSeries{SeriesUID = ESeriesUID.PersonnelManage, Title = "员工管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.PersonnelGroupManage, Title = "员工组管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DeviceManage, Title = "设备管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DeviceTypeManage, Title = "设备类型管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.OfficeOfficeManage, Title = "营业部管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.OfficeCityManage, Title = "城市管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.OfficeProvinceManage, Title = "省份管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.UserManage, Title = "用户管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.UserVIP, Title = "VIP管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.UserCoupon, Title = "优惠券管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.UserIntegral, Title = "用户积分管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DominoInsert, Title = "商品添加", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DominoManage, Title = "商品管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DominoTypeManage, Title = "商品类型管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DominoParams, Title = "商品参数管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.OrderInsert, Title = "订单添加", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.OrderManage, Title = "订单管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.MessageInsert, Title = "留言添加", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.MessageManage, Title = "留言管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.WordInsert, Title = "文章添加", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.WordManage, Title = "文章管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.ImageInsert, Title = "相册添加", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.ImageManage, Title = "相册管理", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
        };

        private readonly CImage[] IMAGES = new CImage[]
        {
            new CImage{StudyUID = EStudyUID.Personnel, SeriesUID = ESeriesUID.PersonnelManage, ImageUID = EImageUID.PersonnelManage},
            new CImage{StudyUID = EStudyUID.Personnel, SeriesUID = ESeriesUID.PersonnelGroupManage, ImageUID = EImageUID.PersonnelGroupManage},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceManage, ImageUID = EImageUID.DeviceManage},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceTypeManage, ImageUID = EImageUID.DeviceTypeManage},
            new CImage{StudyUID = EStudyUID.Office, SeriesUID = ESeriesUID.OfficeOfficeManage, ImageUID = EImageUID.OfficeOfficeManage},
            new CImage{StudyUID = EStudyUID.Office, SeriesUID = ESeriesUID.OfficeCityManage, ImageUID = EImageUID.OfficeCityManage},
            new CImage{StudyUID = EStudyUID.Office, SeriesUID = ESeriesUID.OfficeProvinceManage, ImageUID = EImageUID.OfficeProvinceManage},
            new CImage{StudyUID = EStudyUID.User, SeriesUID = ESeriesUID.UserManage, ImageUID = EImageUID.UserManage},
            new CImage{StudyUID = EStudyUID.User, SeriesUID = ESeriesUID.UserVIP, ImageUID = EImageUID.UserVIP},
            new CImage{StudyUID = EStudyUID.User, SeriesUID = ESeriesUID.UserCoupon, ImageUID = EImageUID.UserCoupon},
            new CImage{StudyUID = EStudyUID.User, SeriesUID = ESeriesUID.UserIntegral, ImageUID = EImageUID.UserIntegral},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.DominoInsert, ImageUID = EImageUID.DominoInsert},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.DominoManage, ImageUID = EImageUID.DominoManage},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.DominoTypeManage, ImageUID = EImageUID.DominoTypeManage},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.DominoParams, ImageUID = EImageUID.DominoParams},
            new CImage{StudyUID = EStudyUID.Order, SeriesUID = ESeriesUID.OrderInsert, ImageUID = EImageUID.OrderInsert},
            new CImage{StudyUID = EStudyUID.Order, SeriesUID = ESeriesUID.OrderManage, ImageUID = EImageUID.OrderManage},
            new CImage{StudyUID = EStudyUID.Message, SeriesUID = ESeriesUID.MessageInsert, ImageUID = EImageUID.MessageInsert},
            new CImage{StudyUID = EStudyUID.Message, SeriesUID = ESeriesUID.MessageManage, ImageUID = EImageUID.MessageManage},
            new CImage{StudyUID = EStudyUID.Word, SeriesUID = ESeriesUID.WordInsert, ImageUID = EImageUID.WordInsert},
            new CImage{StudyUID = EStudyUID.Word, SeriesUID = ESeriesUID.WordManage, ImageUID = EImageUID.WordManage},
            new CImage{StudyUID = EStudyUID.Image, SeriesUID = ESeriesUID.ImageInsert, ImageUID = EImageUID.ImageInsert},
            new CImage{StudyUID = EStudyUID.Image, SeriesUID = ESeriesUID.ImageManage, ImageUID = EImageUID.ImageManage}
        };

        private static PermissionList permission;
        public static PermissionList GetInstance()
        {
            if(permission == null)  permission = new PermissionList();

            return permission;
        }

        private PermissionList()
        {
            CreatePermissions();
        }

        private List<String> Permissions = new List<String>();

        //DEBUG
        public void CreatePermissions()
        {

            HttpResponse response = API.SelectPersonnelGroupPermission(App.LoginInfo.PersonnelGroupUID);
            if (response.S == HttpResponse.Success)
            {
                List<CPersonnelGroupPermission> list = JSON.parse<List<CPersonnelGroupPermission>>(response.D);
                foreach(var item in list)
                {
                    Permissions.Add(item.PermissionUID);
                }
            }
            else
            {
                MessageBox.Show(response.D, "ERROR", MessageBoxButton.OK);
            }
        }

        public List<CStudy> GetStudies()
        {
            List<CStudy> studies = new List<CStudy>();

            foreach (String permission in Permissions)
            {
                foreach (CImage image in IMAGES)
                {
                    if (permission == image.ImageUID)
                    {
                        bool isExist = false;

                        foreach (CStudy study in studies)
                        {
                            if (image.StudyUID == study.StudyUID)
                            {
                                isExist = true;
                                break;
                            }
                        }

                        if (!isExist)
                        {
                            studies.Add(STUDIES[(int)image.StudyUID]);
                        }

                        break;
                    }
                }
            }

            return studies;
        }

        public List<CSeries> GetSerieses(EStudyUID pEStudyUID)
        {
            List<CSeries> serieses = new List<CSeries>();

            foreach (String permission in Permissions)
            {
                foreach (CImage image in IMAGES)
                {
                    if (permission == image.ImageUID && pEStudyUID == image.StudyUID)
                    {
                        bool isExist = false;

                        foreach (CSeries series in serieses)
                        {
                            if (image.SeriesUID == series.SeriesUID)
                            {
                                isExist = true;
                                break;
                            }
                        }

                        if (!isExist)
                        {
                            serieses.Add(SERIESES[(int)image.SeriesUID]);
                        }

                        break;
                    }
                }
            }

            return serieses;
        }

        public List<CImage> GetImages(ESeriesUID pESeriesUID)
        {
            List<CImage> images = new List<CImage>();

            foreach (String permission in Permissions)
            {
                foreach (CImage image in IMAGES)
                {
                    if (permission == image.ImageUID && pESeriesUID == image.SeriesUID)
                    {
                        bool isExist = false;

                        foreach (CImage img in images)
                        {
                            if (image.ImageUID == img.ImageUID)
                            {
                                isExist = true;
                                break;
                            }
                        }

                        if (!isExist)
                        {
                            images.Add(image);
                        }

                        break;
                    }
                }
            }

            return images;
        }

        public EStudyUID GetStudyUID(ESeriesUID pESeriesUID)
        {
            foreach (CImage image in IMAGES)
            {
                if (image.SeriesUID == pESeriesUID)
                {
                    return image.StudyUID;
                }
            }

            return EStudyUID.None;
        }

        public EStudyUID GetStudyUID(String pEImageUID)
        {
            foreach (CImage image in IMAGES)
            {
                if (image.ImageUID == pEImageUID)
                {
                    return image.StudyUID;
                }
            }

            return EStudyUID.None;
        }

        public ESeriesUID GetSeriesUID(String pEImageUID)
        {
            foreach (CImage image in IMAGES)
            {
                if (image.ImageUID == pEImageUID)
                {
                    return image.SeriesUID;
                }
            }

            return ESeriesUID.None;
        }

        public CStudy GetStudy(EStudyUID pEStudyUID)
        {
            CStudy study = null;

            foreach (var std in STUDIES)
            {
                if (std.StudyUID == pEStudyUID)
                {
                    study = std;
                    break;
                }
            }

            return study;
        }

        public CSeries GetSeries(ESeriesUID pESeriesUID)
        {
            CSeries series = null;

            foreach (var srs in SERIESES)
            {
                if (srs.SeriesUID == pESeriesUID) 
                {
                    series = srs;
                    break;
                }
            }

            return series;
        }
    }
}
